<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      :label-width="formConfig.labelWidth ? formConfig.labelWidth + 'px' : '100px'"
      ref="formRef"
      class="demo-form-inline"
    >
      <el-row v-if="formFields.length > 0" :gutter="rowConfig.gutter || 20">
        <el-col v-for="item in formFields" :span="item.span || 24">
          <el-form-item :label="item.label" :prop="item.prop">
            <!-- 输入框 -->
            <el-input
              v-if="item.type == 'input'"
              v-model="form[item.prop]"
              :placeholder="`请输入${item.label}`"
            />
            <!-- 数字输入框 -->
            <el-input-number
              v-if="item.type == 'number'"
              v-model="form[item.prop]"
              :style="{width:item.width||'100%'}"
              :size="item.size || 'large'"
              :placeholder="`请输入${item.label}`"
            />
            <!-- 文本输入框 -->
            <el-input
              v-if="item.type == 'textarea'"
              type="textarea"
              :style="{width:item.width||'100%'}"
              v-model="form[item.prop]"
              :placeholder="`请输入${item.label}`"
            />
            <!-- 多选框 -->
            <el-checkbox-group
              v-if="item.type == 'checkbox' && item.group && item.group.length > 0"
              v-model="form[item.prop]"
            >
              <el-checkbox
                v-for="check in item.group"
                :label="check.label"
                :value="check.value"
                :disabled="check.disabled || false"
              />
            </el-checkbox-group>
            <!-- 单选 -->
            <el-radio-group
              v-if="item.type == 'radio' && item.group && item.group.length > 0"
              v-model="form[item.prop]"
            >
              <el-radio
                v-for="radio in item.group"
                :value="radio.value"
                :disabled="radio.disabled"
                >{{ radio.label }}</el-radio
              >
            </el-radio-group>
            <!-- 选择器 -->
            <el-select
              v-if="item.type == 'select' && item.options && item.options.length > 0"
              v-model="form[item.prop]"
              :placeholder="`请选择${item.label}`"
              :style="{width:item.width||'100%'}"
            >
              <el-option
                v-for="option in item.options"
                :key="option.value"
                :label="option.label"
                :value="option.value"
                :disabled="item.disabled"
              />
            </el-select>
            <!-- 开关 -->
            <el-switch
              v-if="item.type == 'switch'"
              v-model="form[item.prop]"
              :active-text="item.active || '开'"
              :inactive-text="item.inactive || '关'"
            />
            <!-- 日期选择器 -->
            <el-date-picker
              v-if="item.type == 'datePicker'"
              v-model="form[item.prop]"
              :style="{width:item.width||'100%'}"
              :type="item.dataType || 'date'"
              :format="item.format || 'YYYY-MM-DD'"
              placeholder="请选择日期"
            />
            <!-- 上传 -->
            <el-upload
              v-if="item.type == 'upload'"
              v-model:file-list="form[item.prop]"
              :list-type="uploadConfig.pictureCard"
              action="#"
              :show-file-list="true"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              :on-change="handleChange"
            >
              <img v-if="uploadConfig.imageUrl" :src="uploadConfig.imageUrl" />
              <div v-else class="avatar-uploader-icon">
                <el-icon><Plus /></el-icon>
              </div>
              <!-- <template #file="{ file }">
                <div :style="{width:uploadConfig.width,height:uploadConfig.height}" >
                     <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                </div>
              </template> -->
            </el-upload>
            <!-- 城市联动:https://www.npmjs.com/package/element-china-area-data?activeTab=readme -->
            <el-cascader
              v-if="item.type == 'cascader'"
              :size="item.size || 'large'"
              :placeholder="`请选择${item.label}`"
              :style="{ width: item.width || '100%' }"
              v-model="form[item.prop]"
              :options="item.options"
            ></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'

const form = ref({})
const formRef = ref(null)
//存储图片
const props = defineProps({
  rowConfig: {
    type: Object,
    default: {
      gutter: 20
    }
  },
  formConfig: {
    type: Object,
    default: {
      labelWidth: '120'
    }
  },
  formFields: {
    type: Array,
    default: () => []
  },
  rules: {
    type: Object,
    default: () => {}
  },
  uploadConfig: {
    type: Object,
    default: {
      width: '100px',
      height: '100px',
      imageUrl: '',
      pictureCard: 'picture-card'
    }
  }
})

//设置传过来的表单信息，并设置验证
const init = () => {
  if (props.formFields.length > 0) {
    //给form设置字段，有默认值的设置默认值
    props.formFields.map((item) => {
      form.value[item.prop] =
        item.defaultValue !== '' && item.defaultValue !== undefined ? item.defaultValue : ''
    })
  }
  console.log(form.value, '叫着夏天')
}
init()

//验证表单
const validateForm = () => {
  return formRef.value.validate((valid) => {
    if (valid) {
      return true
    } else {
      return false
    }
  })
}

//文件上传成功时的钩子
const handleAvatarSuccess = () => {}
//上传文件之前的钩子
const beforeAvatarUpload = () => {}
//文件状态改变时的钩子
const handleChange = (file) => {
  console.log(file)
}
//导出方法
defineExpose({
  validateForm,
  form
})
</script>

<style lang="scss" scoped>
.avatar-uploader-icon {
  font-size: 30px;
}
</style>
